using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Configuration;
using CE.Global;
using CE.User;

public partial class car_insurance_set_insurance_reminder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        //{
        //    if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
        //        Convert.ToInt32(CE.Global.CEUserType.Customer))
        //    {
        //        Response.Redirect("~/User/LoginAndRegister.aspx");
        //    }
        //}
        //else
        //{
        //    Response.Redirect("~/User/LoginAndRegister.aspx");
        //}


        if (!IsPostBack)
        {
            txtFullName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtFullName.ClientID + "','" + rfvFullName.ClientID + "','" + revFullName.ClientID + "');");
            txtEmailAddress.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailAddress.ClientID + "','" + rfvEmailAddress.ClientID + "','" + revEmailAddress.ClientID + "');");

            ddlCarMonth.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCarMonth.ClientID + "','" + rfvCarMonth.ClientID + "');");
            //ddlPAMonth.Attributes.Add("onblur", "ShowValidationEffect('" + ddlPAMonth.ClientID + "','" + rfvPAMonth.ClientID + "');");
            //ddlMedicalMonth.Attributes.Add("onblur", "ShowValidationEffect('" + ddlMedicalMonth.ClientID + "','" + rfvMedicalMonth.ClientID + "');");
            LogUserViewedHistory();

            txtFullName.Text = "";
            txtEmailAddress.Text = "";
            txtMobileNo.PhoneNo = "";
            ddlCarMonth.SelectedIndex = 0;

            if (Request.QueryString["sc"] != null && Request.QueryString["sc"].ToString() == "1")
            {
                divThankYou.Visible = true;
                divReminder.Visible = false;
                lblSetInsMessage.Text = "Thank you for your request. <br /> We will send reminder to your email.";
            }
            else
            {
                divReminder.Visible = true;
                divThankYou.Visible = false;
            }
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Name
        if (rfvFullName.Enabled && rfvFullName.ValidationGroup == strValidationGroup)
        {
            rfvFullName.Validate();
            if (rfvFullName.IsValid == false)
            {
                txtFullName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtFullName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtFullName.ControlStyle.CssClass = "";
        }

        //Email Address
        if (rfvEmailAddress.Enabled && rfvEmailAddress.ValidationGroup == strValidationGroup)
        {
            rfvEmailAddress.Validate();
            if (rfvEmailAddress.IsValid == false)
            {
                txtEmailAddress.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtEmailAddress.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtEmailAddress.ControlStyle.CssClass = "";
        }

        if (revEmailAddress.Enabled && revEmailAddress.ValidationGroup == strValidationGroup)
        {
            if (rfvEmailAddress.IsValid)
            {
                revEmailAddress.Validate();
                if (revEmailAddress.IsValid == false)
                {
                    txtEmailAddress.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtEmailAddress.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtEmailAddress.ControlStyle.CssClass = "";
        }
        txtMobileNo.ChangeValidationEffectServerSide(strValidationGroup);
        //if (ddlCarMonth.Text != "-1")
        //{
        //    if (ddlCarMonth.Text != "-1")
        //        rfvCarMonth.Enabled = true;
        //    else
        //        rfvCarMonth.Enabled = false;
        //}

        //Car Month
        if (rfvCarMonth.Enabled && rfvCarMonth.ValidationGroup == strValidationGroup)
        {
            rfvCarMonth.Validate();
            if (rfvCarMonth.IsValid == false)
            {
                ddlCarMonth.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlCarMonth.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlCarMonth.ControlStyle.CssClass = "";
        }

        //Personal Accident
        //if (rfvPAMonth.Enabled && rfvPAMonth.ValidationGroup == strValidationGroup)
        //{
        //    rfvPAMonth.Validate();
        //    if (rfvPAMonth.IsValid == false)
        //    {
        //        ddlPAMonth.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        ddlPAMonth.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlPAMonth.ControlStyle.CssClass = "";
        //}

        //Medical Month
        //if (rfvMedicalMonth.Enabled && rfvMedicalMonth.ValidationGroup == strValidationGroup)
        //{
        //    rfvMedicalMonth.Validate();
        //    if (rfvMedicalMonth.IsValid == false)
        //    {
        //        ddlMedicalMonth.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        ddlMedicalMonth.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlMedicalMonth.ControlStyle.CssClass = "";
        //}
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("SIR", Request, "Set insurance reminder.");
    }

    protected void btnSetReminder_Click(object sender, EventArgs e)
    {
        Page.Validate("reminder");
        ChangeValidationEffectServerSide("reminder");
        if (Page.IsValid)
        {
            UserSIR _usersir = new UserSIR();
            UserSIRDB _usersirdb = new UserSIRDB();
            _usersir = _usersirdb.GetSIR(txtEmailAddress.Text.Trim(), txtMobileNo.GetPhoneNo());
            if (_usersir == null)
            {
                _usersir = new UserSIR();
                _usersir.SIRID = 0;
                _usersir.FullName = txtFullName.Text;
                _usersir.Email = txtEmailAddress.Text;
                _usersir.MobileNumber = txtMobileNo.GetPhoneNo();
                _usersir.CarRenewalMonth = Convert.ToInt16(ddlCarMonth.Text);
                //_usersir.PARenewalMonth = Convert.ToInt16(ddlPAMonth.Text);
                //_usersir.MedicalRenewalMonth = Convert.ToInt16(ddlMedicalMonth.Text);
                _usersir.Status = true;

                _usersir.SIRID = _usersirdb.Insert(_usersir);
                if (_usersir.SIRID > 0)
                {
                    string _emailTo = CE.Configuration.MyConfigurationManager.GetAppSettingValue("SIRMessageReceiver");
                    string _emailBody = "<br/><table><tr><td>Name</td><td>:</td><td>" + txtFullName.Text + "</td></tr>";
                    _emailBody += "<tr><td>Email</td><td>:</td><td>" + txtEmailAddress.Text + "</td></tr>";
                    _emailBody += "<tr><td>Mobile</td><td>:</td><td>" + txtMobileNo.GetPhoneNo() + "</td></tr>";
                    _emailBody += "<tr><td>Car Insurance Renewal Month</td><td>:</td><td>" + ddlCarMonth.SelectedItem.Text + "</td></tr></table><br/><br/>Thank you.";
                    // by (" + txtFullName.Text + ") submitted at " + CE.Global.General.GetUTCDateTime().ToString()
                    CE.Mail.MyMailManager.SendCRMMessage(_emailTo, "Car Insurance Reminder (" + txtFullName.Text + ")", _emailBody);
                }
                else
                {
                    lblSetInsMessage.Text = "Transaction failed. Please try again later.";
                }
            }
            else
            {
                _usersir.FullName = txtFullName.Text;
                _usersir.CarRenewalMonth = Convert.ToInt16(ddlCarMonth.Text);
                _usersir.SIRID = _usersirdb.Insert(_usersir);
            }

            if (_usersir != null && _usersir.SIRID > 0)
            {
                lblSetInsMessage.Text = "Thank you for your request. We will send reminder to your email.";
                txtFullName.ReadOnly = true;
                txtEmailAddress.ReadOnly = true;
                txtMobileNo.IsEnabled = false;
                btnSetReminder.Enabled = false;
                Response.Redirect("set-insurance-reminder.aspx?sc=1");
            }
            else
            {
                lblSetInsMessage.Text = "Transaction failed. Please try again later.";
            }
        }
    }

}
